package com.nova.android.tvlauncher.ui.view

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.content.ContextCompat.startActivity
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.nova.android.tvlauncher.AppCache
import com.nova.android.tvlauncher.constants.ConstantsAtv.PACKAGE_NAME_MAIN_SETTING
import com.nova.android.tvlauncher.databinding.ViewMainTopRightAtvBinding
import com.nova.android.tvlauncher.ext.launchCommonRightPanelAtvActivity
import com.nova.android.tvlauncher.ext.showCurrentTime
import com.nova.android.tvlauncher.ui.activity.atv.CommonRightPanelAtvActivity
import com_github_lib_base.currentActivity
import com_github_lib_base.ext.clickNoRepeat
import com_github_lib_base.ext.openApp

class MainTopRightAtvView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    // 初始化视图
    private var binding: ViewMainTopRightAtvBinding =
        ViewMainTopRightAtvBinding.inflate(LayoutInflater.from(context), this, true)

    init {
// 允许子视图超出边界，以支持缩放动画
        clipChildren = false
        clipToPadding = false
        setupListeners()
    }

    fun mainTopRightBinding() = binding

    private fun setupListeners() {
        currentActivity?.let { binding.tvTime.showCurrentTime((it as FragmentActivity).lifecycleScope) }

        binding.tvNotification.clickNoRepeat {
            currentActivity?.let {
                CommonRightPanelAtvActivity.Companion.SYS_NOTIFICATION_FRAGMENT.launchCommonRightPanelAtvActivity()
            }
        }

        binding.tvSetting.clickNoRepeat {
            currentActivity?.let { // 跳转到系统设置页面
                //startActivity(Intent(Settings.ACTION_SETTINGS)) }
                val settingPath = AppCache.configLocalDto?.settingPath
                if(settingPath!=null){
                    settingPath.openApp()
                }else{
                    PACKAGE_NAME_MAIN_SETTING.openApp()
                }

            }
        }

    }


}
